package com.ymatou.iapi.sellerproduct.testcase;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellerproduct.service.AutoRefreshProductsMessageHandlerAppCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.mongo.ProductBasicInfoWapper;
import com.ymttest.database.mongo.ProductDetailModelWapper;
import com.ymttest.database.mongo.ProductListingModelWapper;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.YMTDateUtil;


@TestSuite
public class Ts_AutoRefreshProductsMessageHandlerApp{
	private static AutoRefreshProductsMessageHandlerAppCall autoRefreshProductsMessageHandlerAppCall;
	private static Integer sellerId = Integer.valueOf(EnvSetup.getData("productsellerid"));
	private static String sellerName = EnvSetup.getData("productsellername");	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商家登录商家后台,重新上架商家所有符合条件商品(APP端)");
	}

	@Before
	public void caseUp() {
		autoRefreshProductsMessageHandlerAppCall=new AutoRefreshProductsMessageHandlerAppCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(autoRefreshProductsMessageHandlerAppCall.getOpurl());
	}
		

	
	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AmendProductBrandAndCategoryApp_001() {
		Logger.start(true,"商家登录商家后台,重新上架商家所有符合条件商品(APP端)");
		try {
			//TODO 补全case代码
			Date startTime = (new SimpleDateFormat("yyyy-MM-dd")).parse(YMTDateUtil.getBeforeOrNextDay(-1));
			Date endTime = (new SimpleDateFormat("yyyy-MM-dd")).parse(YMTDateUtil.getBeforeOrNextDay(1));
			String startTimeforsql = YMTDateUtil.getBeforeOrNextHour(0, "yyyy-MM-dd HH");	//sql没有时差
			String endTimeforsql = YMTDateUtil.getBeforeOrNextHour(168, "yyyy-MM-dd HH");
			
			//发布一个6天前立即上架商品
			String productID1 = addNewProduct10dayago(sellerId, sellerName);
//			String productID2 = addNewProducthistory(sellerId, sellerName);
//			for (int i = 0; i < 10; i++) {
//				 int num =  i; 
//				 addNewProducthistory(sellerId, sellerName);
//			}

			//更新登陆记录为过期
			YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
			ymtProductsIWapper.updateSellerLoginByUserId(startTime,endTime,sellerId);
			
			
			//Call 登陆自动上架
			Map<String,Object> maplist = new HashMap<String, Object>();		    
			maplist.put("UserID",sellerId);	
			JSONObject autoRefreshProductsMessageHandlerAppBean =  JsonBeanHelper.getJsonBean("/com.ymatou.iapi.sellerproduct/AutoRefreshProductsMessageHandlerCall/autorefreshproductsmessagehandlercalltpl.json");
			autoRefreshProductsMessageHandlerAppBean = JsonBeanHelper.updateJsonBean(autoRefreshProductsMessageHandlerAppBean, maplist);			
			autoRefreshProductsMessageHandlerAppCall.setData(autoRefreshProductsMessageHandlerAppBean);
			autoRefreshProductsMessageHandlerAppCall.callService();	     	 
			//返回报文断言
			String result = autoRefreshProductsMessageHandlerAppCall.getReturnData();
			autoRefreshProductsMessageHandlerAppreturncode(new JSONObject(result));

			YMTDateUtil.waitTime(10);
			
			//SQL         
			List<Map> productIDsqllist1 = ymtProductsIWapper.selectProductByProductIdforMap(productID1);
			String svalidStartbysql1 = (new SimpleDateFormat("yyyy-MM-dd HH")).format(productIDsqllist1.get(0).get("validStart"));
			String svalidEndbysql1 = (new SimpleDateFormat("yyyy-MM-dd HH")).format(productIDsqllist1.get(0).get("validEnd"));	
			//断言
			Logger.verifyEquals(startTimeforsql, svalidStartbysql1, "SQL对比商品开始销售时间");
			Logger.verifyEquals(endTimeforsql, svalidEndbysql1, "SQL对比商品结束销售时间");
			
			
			if (YmtMongoDriverFactroy.isOldMongoCheck()){
				//Mango
				Map<String, Object> productBasicInfo = new ProductBasicInfoWapper().getByProductId(productID1); 						
				AssertService.assertTime(YMTDateUtil.getDate(), (Date)productBasicInfo.get("ValidStart"), 10000);
				AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(7), (Date)productBasicInfo.get("ValidEnd"), 10000);
			}
					
			if (YmtMongoDriverFactroy.isNewMongoCheck()){
				//New Mongo
				//ProductDetailModel
				Map<String,Object> productDetailModel = new ProductDetailModelWapper().getProductId(productID1);
				AssertService.assertTime(YMTDateUtil.getDate(), (Date)productDetailModel.get("ValidStart"), 10000);
				AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(7), (Date)productDetailModel.get("ValidEnd"), 10000);
												
				//ProductListingModel
				Map<String,Object> productListingModel = new ProductListingModelWapper().getProductId(productID1);
				AssertService.assertTime(YMTDateUtil.getDate(), (Date)productListingModel.get("ValidStart"), 10000);
				AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(7), (Date)productListingModel.get("ValidEnd"), 10000);				
			}
			
						
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AmendProductBrandAndCategoryApp_002() {
		Logger.start(false,"5天前的商品不会自动上架");
		try {
			//TODO 补全case代码
			Date startTime = (new SimpleDateFormat("yyyy-MM-dd")).parse(YMTDateUtil.getBeforeOrNextDay(-1));
			Date endTime = (new SimpleDateFormat("yyyy-MM-dd")).parse(YMTDateUtil.getBeforeOrNextDay(1));
			
			//发布一个6天前立即上架商品
			JSONObject addNewProducthistoryObject = SellerProductCallService.addNewProduct5dayago(sellerId, sellerName, null);
			Logger.verifyEquals(addNewProducthistoryObject.getInt("Code"), 200, "响应报文-返回码");
			//获取返回报文的ProductID值
			String productID1 =  addNewProducthistoryObject.getJSONObject("Data").getString("ProductID");
		
			//更新登陆记录为过期
			YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
			ymtProductsIWapper.updateSellerLoginByUserId(startTime,endTime,sellerId);
			
			//Call 登陆自动上架
			Map<String,Object> maplist = new HashMap<String, Object>();		    
			maplist.put("UserID",sellerId);	
			JSONObject autoRefreshProductsMessageHandlerAppBean =  JsonBeanHelper.getJsonBean("/com.ymatou.iapi.sellerproduct/AutoRefreshProductsMessageHandlerCall/autorefreshproductsmessagehandlercalltpl.json");
			autoRefreshProductsMessageHandlerAppBean = JsonBeanHelper.updateJsonBean(autoRefreshProductsMessageHandlerAppBean, maplist);			
			autoRefreshProductsMessageHandlerAppCall.setData(autoRefreshProductsMessageHandlerAppBean);
			autoRefreshProductsMessageHandlerAppCall.callService();	     	 
			//返回报文断言
			String result = autoRefreshProductsMessageHandlerAppCall.getReturnData();
			autoRefreshProductsMessageHandlerAppreturncode(new JSONObject(result));
			
			YMTDateUtil.waitTime(10);
			
			//SQL         
			List<Map> productIDsqllist1 = ymtProductsIWapper.selectProductByProductIdforMap(productID1);
			//断言
			AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(-5), (Date)productIDsqllist1.get(0).get("validStart"), 1000);
			AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(2), (Date)productIDsqllist1.get(0).get("validEnd"), 1000);
			
			if (YmtMongoDriverFactroy.isOldMongoCheck()){
				//Mango
				Map<String, Object> productBasicInfo = new ProductBasicInfoWapper().getByProductId(productID1); 						
				AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(-5), (Date)productBasicInfo.get("ValidStart"), 1000);
				AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(2), (Date)productBasicInfo.get("ValidEnd"), 1000);
			}
					
			if (YmtMongoDriverFactroy.isNewMongoCheck()){
				//New Mongo
				//ProductDetailModel
				Map<String,Object> productDetailModel = new ProductDetailModelWapper().getProductId(productID1);
				AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(-5), (Date)productDetailModel.get("ValidStart"), 1000);
				AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(2), (Date)productDetailModel.get("ValidEnd"), 1000);
												
				//ProductListingModel
				Map<String,Object> productListingModel = new ProductListingModelWapper().getProductId(productID1);
				AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(-5), (Date)productListingModel.get("ValidStart"), 1000);
				AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(2), (Date)productListingModel.get("ValidEnd"), 1000);				
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AmendProductBrandAndCategoryApp_003() {
		Logger.start(true,"过期商品自动上架");
		try {
			//TODO 补全case代码
			Date startTime = (new SimpleDateFormat("yyyy-MM-dd")).parse(YMTDateUtil.getBeforeOrNextDay(-1));
			Date endTime = (new SimpleDateFormat("yyyy-MM-dd")).parse(YMTDateUtil.getBeforeOrNextDay(1));
			String startTimeforsql = YMTDateUtil.getBeforeOrNextHour(0, "yyyy-MM-dd HH");	//sql没有时差
			String endTimeforsql = YMTDateUtil.getBeforeOrNextHour(168, "yyyy-MM-dd HH");
			
			//发布一个6天前立即上架商品
			JSONObject addNewProducthistoryObject = SellerProductCallService.addNewProduct10dayago(sellerId, sellerName, null);
			Logger.verifyEquals(addNewProducthistoryObject.getInt("Code"), 200, "响应报文-返回码");
			//获取返回报文的ProductID值
			String productID1 =  addNewProducthistoryObject.getJSONObject("Data").getString("ProductID");
			
			
			//更新登陆记录为过期
			YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
			ymtProductsIWapper.updateSellerLoginByUserId(startTime,endTime,sellerId);
			
			//Call 登陆自动上架
			Map<String,Object> maplist = new HashMap<String, Object>();		    
			maplist.put("UserID",sellerId);	
			JSONObject autoRefreshProductsMessageHandlerAppBean =  JsonBeanHelper.getJsonBean("/com.ymatou.iapi.sellerproduct/AutoRefreshProductsMessageHandlerCall/autorefreshproductsmessagehandlercalltpl.json");
			autoRefreshProductsMessageHandlerAppBean = JsonBeanHelper.updateJsonBean(autoRefreshProductsMessageHandlerAppBean, maplist);			
			autoRefreshProductsMessageHandlerAppCall.setData(autoRefreshProductsMessageHandlerAppBean);
			autoRefreshProductsMessageHandlerAppCall.callService();	     	 
			//返回报文断言
			String result = autoRefreshProductsMessageHandlerAppCall.getReturnData();
			autoRefreshProductsMessageHandlerAppreturncode(new JSONObject(result));
			
			YMTDateUtil.waitTime(10);	
			
			//SQL         
			List<Map> productIDsqllist1 = ymtProductsIWapper.selectProductByProductIdforMap(productID1);
			String svalidStartbysql1 = (new SimpleDateFormat("yyyy-MM-dd HH")).format(productIDsqllist1.get(0).get("validStart"));			
			String svalidEndbysql1 = (new SimpleDateFormat("yyyy-MM-dd HH")).format(productIDsqllist1.get(0).get("validEnd"));	
			//断言
			Logger.verifyEquals(startTimeforsql, svalidStartbysql1, "SQL对比商品开始销售时间");
			Logger.verifyEquals(endTimeforsql, svalidEndbysql1, "SQL对比商品结束销售时间");
			

			if (YmtMongoDriverFactroy.isOldMongoCheck()){
				//Mango
				Map<String, Object> productBasicInfo = new ProductBasicInfoWapper().getByProductId(productID1); 						
				AssertService.assertTime(YMTDateUtil.getDate(), (Date)productBasicInfo.get("ValidStart"), 10000);
				AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(7), (Date)productBasicInfo.get("ValidEnd"), 10000);
			}
					
			if (YmtMongoDriverFactroy.isNewMongoCheck()){
				//New Mongo
				//ProductDetailModel
				Map<String,Object> productDetailModel = new ProductDetailModelWapper().getProductId(productID1);
				AssertService.assertTime(YMTDateUtil.getDate(), (Date)productDetailModel.get("ValidStart"), 10000);
				AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(7), (Date)productDetailModel.get("ValidEnd"), 10000);
												
				//ProductListingModel
				Map<String,Object> productListingModel = new ProductListingModelWapper().getProductId(productID1);
				AssertService.assertTime(YMTDateUtil.getDate(), (Date)productListingModel.get("ValidStart"), 10000);
				AssertService.assertTime(YMTDateUtil.getBeforeOrNextDate(7), (Date)productListingModel.get("ValidEnd"), 10000);				
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	
	
	
	

	
	
	/**
	 * 接口返回信息公用信息部分断言（正向Case场景）
	 * @param dataStr 传入报文信息
	 * @throws Exception 错误信息
	 */
	public void autoRefreshProductsMessageHandlerAppreturncode(JSONObject dataStr) throws Exception {
	    try {
			//返回报文断言
			String code =  dataStr.getString("Code");
			Logger.verifyIsContains(200, code, "\"Code\":200");
			String bCode =  dataStr.getString("BCode");
			Logger.verifyIsContains(0, bCode, "\"BCode\":0");
			String msg =  dataStr.getString("Msg");
			Logger.verifyIsContains("", msg, "Msg操作成功");
			String trace =  dataStr.getString("Trace");
			Logger.verifyIsNull(trace, "Trace等于空");			

	    }catch (Exception e) {
	        e.printStackTrace();
	    }
	}
	
		
	/**
	 * 发布一个立即上架商品
	 * @return 商品id
	 * @throws Exception
	 */
	public String addNewProductOnSale(int sellerId, String sellerName) throws Exception {
	    try {	    	
			//发布商品
			JSONObject addNewProductOnSaleObject = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null);
			Logger.verifyEquals(addNewProductOnSaleObject.getInt("Code"), 200, "响应报文-返回码");
			//获取返回报文的ProductID值
			String productID =  addNewProductOnSaleObject.getJSONObject("Data").getString("ProductID");
			Logger.verifyNotNull(productID, "响应报文-商品ID");
			return productID;
	    }catch (Exception e) {  
	        e.printStackTrace();	        
	    }  
	    return null;
	}
	
	/**
	 * 发布一个未上架商品
	 * @return 商品id
	 * @throws Exception
	 */
	public String addNewProductNotOnSale(int sellerId, String sellerName) throws Exception {
	    try {	    	
			//发布商品
			JSONObject addNewProductOnSaleObject = SellerProductCallService.addNewProductNotOnSale(sellerId, sellerName, null);
			Logger.verifyEquals(addNewProductOnSaleObject.getInt("Code"), 200, "响应报文-返回码");
			//获取返回报文的ProductID值
			String productID =  addNewProductOnSaleObject.getJSONObject("Data").getString("ProductID");
			Logger.verifyNotNull(productID, "响应报文-商品ID");
			return productID;
	    }catch (Exception e) {  
	        e.printStackTrace();	        
	    }  
	    return null;
	}
	
	/**
	 * 发布一个6天前自动上架的商品
	 * @return 商品id
	 * @throws Exception
	 */
	public static String addNewProducthistory(int sellerId, String sellerName) throws Exception {
	    try {	    	
			//发布商品
			JSONObject addNewProducthistoryObject = SellerProductCallService.addNewProducthistory(sellerId, sellerName, null);
			Logger.verifyEquals(addNewProducthistoryObject.getInt("Code"), 200, "响应报文-返回码");
			//获取返回报文的ProductID值
			String productID =  addNewProducthistoryObject.getJSONObject("Data").getString("ProductID");
			Logger.verifyNotNull(productID, "响应报文-商品ID");
			return productID;
	    }catch (Exception e) {  
	        e.printStackTrace();	        
	    }  
	    return null;
	}

	/**
	 * 发布一个10天前自动上架的商品
	 * @return 商品id
	 * @throws Exception
	 */
	public static String addNewProduct10dayago(int sellerId, String sellerName) throws Exception {
	    try {	    	
			//发布商品
			JSONObject addNewProducthistoryObject = SellerProductCallService.addNewProduct10dayago(sellerId, sellerName, null);
			Logger.verifyEquals(addNewProducthistoryObject.getInt("Code"), 200, "响应报文-返回码");
			//获取返回报文的ProductID值
			String productID =  addNewProducthistoryObject.getJSONObject("Data").getString("ProductID");
			Logger.verifyNotNull(productID, "响应报文-商品ID");
			return productID;
	    }catch (Exception e) {  
	        e.printStackTrace();	        
	    }  
	    return null;
	}
	
	
	
}